<?php
declare (strict_types = 1);

namespace app\common\validate;

use think\Exception;
use think\Validate;

class Password extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */	
	protected $rule = [
        'ori_password'   =>'require|checkPassword',
        'new_password'   =>'require|min:6',
        're_password'    =>'require|confirm:new_password',
    ];
    
    /**
     * 定义错误信息
     * 格式：'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */	
    protected $message = [
        'ori_password.require' => '原密码不能为空.',
        'ori_password.checkPassword' => '原密码错误.',
        'new_password.require' => '新密码不能为空.',
        'new_password.min'     => '密码至少6个字符.',
        're_password.require'  => '重复密码不能为空.',
        're_password.confirm'  => '两次密码不一致.',
    ];

    // 检测父id是否正确
    protected function checkPassword($value, $rule, $data=[])
    {
        $user = app()->user;
        if ($user->isGuest()) throw new Exception('必须登录.');
        return $user->getIdentity()->validatePassword($value);
    }
}
